<template>
	<view :style="{ height: conentHeight + 'px;' }">
		<view class="top-height">
			<view class="page-top-box">
				<view class="search-box flex ac">
					<u-search
						borderColor="#DADADA"
						bgColor="#ffffff"
						placeholder="搜索项目名称"
						v-model="searchForm.project_name"
						:showAction="false"
						@search="searchFunc"
						@clear="searchFunc"
					></u-search>
					<view class="search-btn flex ac" @click="searchShow = true" :style="searchShow ? 'color:#3B75FF;' : ''">
						筛选
						<text class="iconfont icon-shaixuan" :style="searchShow ? 'color:#3B75FF;' : ''"></text>
					</view>
					<u-button
						v-if="$isPer(per_arr, ['discount_add'])"
						@click="navToAddFunc"
						customStyle="width: 200rpx;height: 64rpx;background: #3B75FF;border-radius: 32rpx;border-color:#F8F8FA;font-weight: 400;font-size: 26rpx;color: #ffffff;margin-left:20rpx;"
						:hairline="false"
						text="发起优惠审批"
					></u-button>
				</view>
			</view>
		</view>
		<view class="list-content-box" :style="{ height: 'calc(100% - ' + topHeight + 'px);' }">
			<z-paging
				@onRefresh="refresh"
				ref="paging"
				:fixed="false"
				v-model="dataList"
				@query="queryList"
				:paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }"
			>
				<template slot="empty">
					<u-empty icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" text=" "></u-empty>
				</template>
				<view class="list-items flex ac flex-wrap" :key="index" v-for="(item, index) in dataList" @click.stop="navToDetailFunc(item)">
					<view class="list-items-title flex ac jb" v-if="$fieldShowFunc('yh_glxm')">
						<view class="title u-line-1">{{ item.project_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('yh_xmlx')">
						<view class="title">项目类型</view>
						<view class="desc u-line-1">{{ item.store_type_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('yh_splx')">
						<view class="title">审批类型</view>
						<view class="desc u-line-1">
							{{ item.type == 1 ? '签约优惠' : '结算优惠' }}
						</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('yh_shzt')">
						<view class="title">审批状态</view>
						<view class="desc u-line-1">
							<template v-if="item.customer_director_review == 1">审核中</template>
							<template v-if="item.customer_director_review == 2">审核通过</template>
							<template v-if="item.customer_director_review == 3">审核拒绝</template>
							<template v-if="item.customer_director_review == 4">已废弃</template>
						</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('yh_yhhcblrl')">
						<view class="title">优惠后成本利润率</view>
						<view class="desc u-line-1">
							{{ item.remaining_cost_profit_ratio ? item.remaining_cost_profit_ratio + '%' : '--' }}
						</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('yh_yjqyje')">
						<view class="title">预计签约金额</view>
						<view class="desc u-line-1">{{ item.expected_signing_amount }}</view>
					</view>
					<!-- 按钮 -->
					<view style="width: 100%">
						<judgeBtn plain :per_arr="per_arr" :btns="btns" objectType="discount" :state="item.customer_director_review" :item="item" @change="btnsChange"></judgeBtn>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 筛选弹窗 -->
		<u-popup :show="searchShow" round="30" @close="searchShow = false" closeable>
			<view class="search-popup-title">全部筛选</view>
			<scroll-view scroll-y class="search-popup-box">
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>项目类型</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.store_type_id.page != 1"
							@click="retractFunc('store_type_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('store_type_id', item)"
							class="search-items-list-item"
							:class="item.id == searchForm.store_type_id ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.store_type_id.storeTypeList"
							:key="index"
						>
							{{ item.store_type_name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('store_type_id')"
							v-if="searchListObj.store_type_id.storeTypeList.length != searchListObj.store_type_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>审批类型</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('type', item)"
							:class="item.value == searchForm.type ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.type.typeList"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">预计签约金额</view>
					<u--input type="number" v-model="searchForm.expected_signing_amount" placeholder="预计签约金额" border="surround"></u--input>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>审批状态</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('customer_director_review', item)"
							:class="item.value == searchForm.customer_director_review ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.customer_director_review.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">优惠后成本利润率</view>
					<u--input type="number" v-model="searchForm.remaining_cost_profit_ratio_min" placeholder="优惠后成本利润率" border="surround"></u--input>
				</view>
			</scroll-view>
			<view class="btn-box flex ac jb">
				<u-button
					@click="reSearchValFunc"
					text="重置"
					customStyle="width: 248rpx;height: 82rpx;background: #242834;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
				<u-button
					@click="searchFunc"
					text="查询"
					customStyle="width: 422rpx;height: 82rpx;background: #3B75FF;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
			</view>
		</u-popup>
		<!-- 操作组件 -->
		<discountOperate :value.sync="per_name" :oper_item="oper_item" @submit="handleSubmit"></discountOperate>
	</view>
</template>

<script>
export default {
	name: 'discountApproval',
	props: {
		conentHeight: {
			default: 0
		},
		option: {
			type: Object,
			default: {}
		}
	},
	provide() {
		return {
			providedData: this.btns
		};
	},
	data() {
		return {
			searchShow: false,
			dataList: [],
			searchForm: {
				project_name: '',
				store_type_id: '',
				type: '',
				expected_signing_amount: '',
				customer_director_review: '',
				remaining_cost_profit_ratio_min: ''
			},
			topHeight: 0,
			searchListObj: {
				store_type_id: {
					storeTypeList: [],
					page: 1,
					num: 5,
					max: 0
				},
				type: {
					typeList: [
						{
							label: '签约优惠',
							value: 1
						},
						{
							label: '结算优惠',
							value: 2
						}
					],
					page: 1,
					num: 5,
					max: 0
				},
				customer_director_review: {
					list: [
						{
							label: '待审核',
							value: 1
						},
						{
							label: '审核通过',
							value: 2
						},
						{
							label: '审核拒绝',
							value: 3
						},
						{
							label: '已废弃',
							value: 4
						}
					],
					page: 1,
					num: 5,
					max: 0
				}
			},
			per_arr: [], //权限列表
			// 操作按钮
			btns: [
				{
					per_name: 'discount_examine',
					func: (item) => {
						if (item.customer_director_review == 1 && item.review_status == 10 && (item.is_retrial == 2 || item.is_retrial == 3)) {
							return true;
						} else {
							return false;
						}
					},
					name: '审核优惠审批'
				},
				{
					per_name: 'discount_discountReviewPassed2',
					func: (item) => {
						if (item.customer_director_review == 1 && item.is_retrial == 3 && item.review_status == 20) {
							return true;
						} else {
							return false;
						}
					},
					name: '复审优惠审批'
				},
				{
					per_name: 'discount_discountInvalidation',
					func: (item) => {
						if (item.customer_director_review != 4) {
							return true;
						} else {
							return false;
						}
					},
					name: '审批作废'
				}
			],
			// 操作相关
			per_name: '',
			oper_item: {}
		};
	},
	mounted() {
		this.getStoreTypeList();
		this.getHeight();
		// 获取权限
		this.$Api
			.getHtmlAuthority({
				path: '/discountApproval'
			})
			.then((res) => {
				console.log(res, 'getHtmlAuthority');
				if (res.code == 200) {
					this.per_arr = res.data.permission;
				}
			});
	},
	watch: {},
	methods: {
		// 刷新修改当前操作item
		refreshItemFunc() {
			console.log('刷新当前数据');
			let oper_item = this.oper_item;
			let index = this.dataList.findIndex((item) => {
				return item.id == oper_item.id;
			});
			if (index == -1) return;
			this.$Apipc
				.discountApprovalDetails({
					discount_id: oper_item.id
				})
				.then((res) => {
					console.log(res, '项目详情');
					if (res.code == 200) {
						this.dataList[index] = res.data;
						this.dataList.splice(0, 0);
					}
				});
		},
		// 操作反馈
		handleSubmit(e) {
			this.refreshItemFunc();
		},
		// 操作事件
		btnsChange(e) {
			console.log(e);
			this.per_name = e.per_name;
			this.oper_item = e.item;
		},
		refresh() {
			this.dataList = [];
		},
		// 重置弹窗筛选项
		reSearchValFunc() {
			this.searchForm = {
				project_name: '',
				store_type_id: '',
				type: '',
				expected_signing_amount: '',
				customer_director_review: '',
				remaining_cost_profit_ratio_min: ''
			};
			this.$nextTick(() => {
				this.searchFunc();
			});
		},
		// 刷新列表
		searchFunc() {
			this.$refs.paging.reload();
			this.searchShow = false;
		},
		getHeight() {
			// 创建节点查询
			let that = this;
			const systemInfo = uni.getSystemInfoSync();
			const query = uni.createSelectorQuery().in(that);
			query.select('.top-height').boundingClientRect();
			query.exec((res) => {
				that.topHeight = res[0].height + systemInfo.statusBarHeight + 20;
			});
		},
		// 去详情
		navToDetailFunc(item) {
			this.oper_item = item;
			uni.navigateTo({
				url: '/pagesB/discountApprovalDetail/discountApprovalDetail?id=' + item.id
			});
		},
		queryList(page, num) {
			this.discountApprovalList(page, num);
		},
		// 优惠审批列表
		discountApprovalList(page, num) {
			uni.showLoading({
				mask: true
			});
			let searchForm = this.searchForm;
			this.$Apipc
				.discountApprovalList({
					page,
					num,
					project_name: searchForm.project_name,
					store_type_id: searchForm.store_type_id,
					type: searchForm.type,
					expected_signing_amount: searchForm.expected_signing_amount,
					customer_director_review: searchForm.customer_director_review,
					remaining_cost_profit_ratio_min: searchForm.remaining_cost_profit_ratio_min
				})
				.then((res) => {
					uni.hideLoading();
					console.log(res, '优惠审批列表');
					if (res.code == 200) {
						this.$refs.paging.complete(res.data.list);
					}
				});
		},
		// 门店类型列表(项目类型)
		getStoreTypeList() {
			this.$Apipc
				.getStoreTypeList({
					page: this.searchListObj.store_type_id.page,
					num: this.searchListObj.store_type_id.num
				})
				.then((res) => {
					console.log(res, '门店类型列表');
					if (res.code == 200) {
						if (this.searchListObj.store_type_id.page == 1) {
							this.searchListObj.store_type_id.storeTypeList = [];
						}
						this.searchListObj.store_type_id.storeTypeList = [...this.searchListObj.store_type_id.storeTypeList, ...res.data.list];
						this.searchListObj.store_type_id.max = res.data.max;
					}
				});
		},
		// 弹窗筛选项
		searchSelectFunc(type, item) {
			console.log(type, item);
			switch (type) {
				case 'store_type_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'type':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				case 'customer_director_review':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				default:
			}
		},
		// 弹窗筛选项查看更多
		lookMoreFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page += 1;
					this.getStoreTypeList();
					break;
				default:
			}
		},
		// 弹窗筛选项列表收起
		retractFunc(type) {
			switch (type) {
				case 'store_type_id':
					this.searchListObj.store_type_id.page = 1;
					this.getStoreTypeList();
					break;
				default:
			}
		},
		// 发起优惠审批
		navToAddFunc() {
			uni.navigateTo({
				url: '/pagesC/projectManagement/addDiscount/addDiscount'
			});
		}
	}
};
</script>

<style lang="scss"></style>
